/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_qna_list.js                            
*  Description  : 마이페이지 qna
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-06  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// Qna 리스트
	function clsQnaList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsQnaList.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_stats");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tbody tr:first").clone(true)
			}
			
			this.setList([]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			if(this.listData.length){
				$layer.find("article.board_list_none").hide();
				$layer.find("article.list01").show();
				
				$layer.find("tbody").empty();
				for(var i=0;i<this.listData.length;i++){
					$layer.find("tbody").append($element.clone(true));
				}
				
				
				$layer.find("tbody tr").css("cursor", "pointer").each(function(n){
					var l = thisClass.listData[n];
					
					$(this).find("td:eq(0)").html(l["번호"]);
					$(this).find("td:eq(1)").html("[" + l["질문대분류명"] + "]");
					$(this).find("td:eq(2)").html(l["질문소분류명"]);
					$(this).find("td:eq(3)").html(l["제목"]);
					$(this).find("td:eq(4)").html(l["등록일"]);
					$(this).find("td:eq(5)").html(l["상태"]);
					$(this).find("td:eq(5)").attr("class", "");
					if(l["상태코드"] == "Y"){
						$(this).find("td:eq(5)").addClass("state_ok");
					}else{
						$(this).find("td:eq(5)").addClass("state_acc");
					}
				});
			}else{
				$layer.find("article.board_list_none").show();
				$layer.find("article.list01").hide();
			}
			
			
		}
		, getQnaKey: function(n){
			return this.listData[n]["질문코드"];
		}
	}
	
	// 서버통신
	function clsComm()
	{
		this.qnaRequest = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.qnaRequest = {};
		}
		, setQnaRequest: function(key, val){
			if(typeof key == "object") this.qnaRequest = key;
			else this.qnaRequest[key] = val;
		}
		, getQnaList: function(){
			var thisClass = this;
			
			//ajax
			var serviceName = "getQnaList";
			var options = {
				
			}
			_gf.ajax({
				"sid": "QA001"
				, "servicename": serviceName
				, "listcount": this.qnaRequest["페이지당수"]
				, "currpageno": this.qnaRequest["페이지"]
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "전체수": ret["msp"]["contents"]["@totalcount"]
				}
				
				var list = [];
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						var reg_date = _util.nvl(obj["reg__date"])+"";
						var objStatus = {
							"Y": "답변완료"
							, "N": "문의접수"
						}
						
						
						list.push({
							"질문코드": _util.nvl(obj["qna__seq"])
							, "번호": 1
							, "질문대분류코드": _util.nvl(obj["qna__type"])
							, "질문대분류명": _util.nvl(obj["qna__name"])
							, "질문소분류코드": _util.nvl(obj["qna__detail__type"])
							, "질문소분류명": _util.nvl(obj["qna__detail__name"])
							, "제목": _util.nvl(obj["title"])
							, "상태코드": _util.nvl(obj["status"])
							, "상태": objStatus[_util.nvl(obj["status"])]
							, "등록일": reg_date.substrEx(0, 4) + "-" + reg_date.substrEx(4, 2) + "-" + reg_date.substrEx(6, 2)
						});
					}
				}
				
				res["리스트"] = list;
				document.objControl.finishQnaList(res);
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objQnaList = null;
		this.objQnaPaging = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objQnaList = new clsQnaList();
			this.objQnaPaging = new _gf.clsPaging($("div.pagenum_area"), 10, 10);
			
			this._beforeStart();
			this._bind();
			
		}
		, _preload: function(){
			_gf.spinStart();
			
			$("ul.pagenum_area").wrap("<div class='pagenum_area'/>").remove();
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				
			}
			
			
			$.ajax({
				url: _ROOT + "/js/mypage_common.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return (_gf.getMyInfoData());
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 문의하기
			$("div#container").on("click", "img[alt='Q&A 문의']", function(){
				document.location.href = _ROOT + "/mypage/my_qna_write.html";
			});
			
			this.objQnaList.$getLayer().on("click", "tr", function(){
				var $layer = thisClass.objQnaList.$getLayer();
				var n = $layer.find("tr").index($(this));
				var qnaKey = thisClass.objQnaList.getQnaKey(n);
				var page = thisClass.objQnaPaging.getPage();
				
				document.location.href = _ROOT + "/mypage/my_qna_view.html?q_seq=" + qnaKey + "&q_page=" + page;
			});
		}
		, _start: function(){
			// 리스트 요청
			this.objQnaPaging.setPage(1);
			this.objComm.setQnaRequest({
				"페이지당수": this.objQnaPaging.getCountPerPage()
				, "페이지": 1
			});
			this.objComm.getQnaList();
		}
		// 리스트 완료
		, finishQnaList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objQnaPaging.setTotalCount(ret["전체수"]);
			this.objQnaPaging.display();
			this.objQnaList.setList(ret["리스트"]);
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});
